package com.itzhanshen.auth.processor.impl;

import com.aliyuncs.utils.StringUtils;
import com.itzhanshen.auth.entity.IntergrationAuthenticationEntity;
import com.itzhanshen.auth.processor.AuthenticationProcessor;
import com.itzhanshen.sys.feign.SysUserFeign;
import com.nineclock.common.entity.Result;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NCException;
import com.nineclock.dto.SysUserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 用户/手机号认证器
 *  检验是否携带 authType是否为空
 */
@Component
public class UsernameAndPasswordAuthenticationProcessor implements AuthenticationProcessor {
    @Autowired
    private SysUserFeign sysUserFeign;
    @Override
    public SysUserDTO authenticate(IntergrationAuthenticationEntity entity) {
         Map<String, String[]> map = entity.getAuthParameters();
        //校验
        if (map == null) {
            throw new NCException(ResponseEnum.USER_NOT_FOUND);
        }
        // 拿出用户名
        String[] usernames = map.get("username");
        System.out.println(usernames[0]);
        // 远程调用
        Result<SysUserDTO> userInfo = sysUserFeign.findUserInfo(usernames[0]);
    /*     // 为相应的结果封装数据
        Result<Map<String, Object>> result = new Result<>();
        M*/
        return userInfo.getData();
    }
    @Override
    public boolean support(IntergrationAuthenticationEntity entity) {
        String authType = entity.getAuthType();
        if (StringUtils.isEmpty(authType)) {
            return true;
        }
        return false;
    }
}
